home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / OT PAPServerSample / SetServerStatusOption.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.5 KB  |  116 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SetServerStatusOption.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include "OpenTransport.h"            // open transport files            
  24. #include "OpenTptAppleTalk.h"
  25. #include <Memory.h>
  26. /*
  27. **    prototype declaration
  28. */
  29. extern OSStatus DoSetServerStatusOption(EndpointRef ep, char *statusStr);
  30.  
  31.  
  32. /*
  33.     global variables
  34.     The nature of the following call is such that you'll want to set the option 
  35.     when the endpoint is in asynch mode.  To handle the asynch case, you must
  36.     declare the global gOptionCompleted.  When an option management call is made
  37.     gOptionCompleted is set to zero.  When the T_OPTMGMTCOMPLETE call is handled
  38.     by the endpoint handler, then gOptionCompleted is set to 1.  Note that the
  39.     sample spin loops until gOptionCompleted is non-zero.  As such, the 
  40.     DoSetServerStatusOption call cannot be made at interrupt time or from a deferred
  41.     task.  The DoSetServerStatusOption can only be made at system task time.
  42.     
  43.     Ensure that gOptionCompleted is used to flag only one outstanding Option 
  44.     Management call at a time.
  45.     
  46. */
  47. extern UInt32    gOptionCompleted;
  48.  
  49. /*
  50.     input parameters
  51.     ep - endpoint on which to set the status string option
  52.     statusstr - C style string of the status string to set
  53.     
  54.     function result - kOTNoError indicates that the option was successfully negotiated
  55.         if the result is negative, then this is the result returned by the call to
  56.         OTOptionManagement.  If the result is positive, then this is the value of 
  57.         the status field on return from the call to 
  58.  
  59. */
  60. OSStatus DoSetServerStatusOption(EndpointRef ep, char *statusStr)
  61.  
  62. {
  63.     UInt8        buf[kOTOptionHeaderSize+256];    // define buffer to allow for 
  64.                                                 // up to a 256 char string
  65.     TOption*    opt;                            // option ptr to make items easier to access
  66.     TOptMgmt    req;
  67.     TOptMgmt    ret;
  68.     UInt32         statusStrLen;
  69.     OSStatus    err;
  70.     Boolean        isAsync = false;
  71.     
  72.     if (OTIsSynchronous(ep) == false)            // check whether ep sync or not
  73.     {
  74.         isAsync = true;
  75.         gOptionCompleted = 0;
  76.     }
  77.     
  78.     statusStrLen = OTStrLength(statusStr);    // get the length of the C string        
  79.     opt = (TOption*)buf;                    // set option ptr to buffer
  80.     req.opt.buf    = buf;
  81.     req.opt.len    = kOTOptionHeaderSize + statusStrLen;
  82.     req.flags    = T_NEGOTIATE;                // negotiate for serverstatus option
  83.  
  84.     ret.opt.buf = buf;
  85.     ret.opt.maxlen = kOTOptionHeaderSize+256;
  86.     
  87.  
  88.     opt->level    = ATK_PAP;                    // dealing with tpi
  89.     opt->name    = OPT_SERVERSTATUS;
  90.     opt->len    = kOTOptionHeaderSize + statusStrLen;
  91.     opt->status = 0;
  92.         // set the serverStr into the value field
  93.     BlockMove(statusStr, (Ptr)&opt->value, statusStrLen);
  94.  
  95.     err = OTOptionManagement(ep, &req, &ret);
  96.     
  97.     if ((isAsync == true) && (err == kOTNoError))
  98.     {
  99.         while (gOptionCompleted == 0)
  100.         {
  101.             // spin in this null loop waiting for the option call to complete.
  102.         }
  103.     }
  104.     
  105.         // if no error then return the option status value
  106.     if (err == kOTNoError)
  107.     {
  108.         if (opt->status != T_SUCCESS)
  109.             err = opt->status;
  110.         else
  111.             err = kOTNoError;
  112.     }
  113.         
  114.     return err;
  115. }
  116.